// pages/home/search/components/user-Item/index.js
const {
  othersApi,
  home
} = require('../../../../../api/index')
Component({
  lifetimes: {
    attached() {
      // console.log('用户信息',this.properties.userInfo);
      if (this.properties.userID) {
        // console.log(this.properties.userID);
        othersApi.getothers({
          UserId: this.properties.userID,
        }).then(res => {
          console.log('');
          if (res.Result.UserCode) {
            res.Result.UserCode = this.highLight1(res.Result.UserCode)
          }
          this.setData({
            userData: res.Result,
          })
        })
      } else {
        // if (this.properties.userInfo.UserCode) {
        //   this.properties.userInfo.UserCode = this.highLight1(this.properties.userInfo.UserCode)
        // }
        this.setData({
          userData: this.properties.userInfo,
        })
      }
      this.setData({
        id: this.properties.userID || this.properties.userInfo.ID
      })
      this.checkFollow();

    }
  },

  /**
   * 组件的属性列表
   */
  properties: {
    userID: {
      type: String,
      value: ''
    },
    key: {
      type: String,
      value: ''
    },
    userInfo: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    userData: {},
    id: '',
    isFollow: false,
    UserCodeList: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    goPage() {
      wx.navigateTo({
        url: `/pages/my/othersPage/index?code=${this.data.userData.UserCode}`,
      })
    },
    highLight1(source) {
      let keywords = this.properties.key
      const reg = new RegExp(keywords, 'gi')
      return source.replace(reg, `<span style="color: #FFC14E">${keywords}</span>`)
    },

    //判断是否关注
    async checkFollow() {
      // 登录了才调用
      let token = wx.getStorageSync("token");
      if (token) {
        // console.log('判断是否关注', this.properties.userInfo);
        if (!this.properties.userInfo?.FollowRelation || this.properties.userInfo?.FollowRelation === 1) {
          this.setData({
            isFollow: false,
          });
        } else {
          this.setData({
            isFollow: true,
          });
        }
        // let res = await home.isfollowtouser({
        //   ID:this.data.id,
        // });
        // this.setData({
        //   isFollow: res.Result,
        // });
      }
    },
    shudLogin() {
      const popup = this.selectComponent('#popupid');
      popup.setData({
        visible: true,
        isNavBar: false
      })
    },
    //关注or取消关注
    async Ifollow() {
      const _this = this;
      try {
        home[this.data.isFollow ? "removefollow" : "addfollow"]({
            ID: this.data.id,
          })
          .then((res) => {
            if (res.StatusCode === 200) {
              this.setData({
                isFollow: !_this.data.isFollow,
              });
              wx.showToast({
                title: _this.data.isFollow ? "关注成功" : "取关成功",
                mask: "true",
              });
            } else {
              wx.showToast({
                title: res.Message,
                mask: "true",
                icon: 'error'
              });
            }
          })
          .catch((err) => {
            if (err.statusCode === 401) {
              _this.shudLogin();
            }
          });
      } catch (error) {
        console.log(error);
      }
    },
  }
})